#include <iostream>
#include <sstream>

#include <gtest/gtest.h>

#include "melon/client/thrift_client_pool.h"
#include "cloud_control/adapters/cloud_control_action_adapter.h"

using melon::client::ClientPool;

using namespace cc;

TEST(CloudControlAction, StartAction) {
  CloudControlAction action;
  action.phone_key = "18101052049";
  action.pc_qid = 267981762;
  action.pc_mid = "6b9185a70d3017aa109624844b6af671";
  action.body = "recycle rubbish";
  int64_t action_id = CloudControlActionAdapter::Instance().StartAction(action, 3);

  sleep(1);
  CloudControlResult result;
  result.action_id = action_id;
  result.body = "100M recycled.";
  CloudControlActionAdapter::Instance().ReportResult(result, 3);
}

TEST(CloudControlAction, ReportResult) {
  CloudControlResult result;
  result.phone_key = "18101052049";
  result.action_id = 5002;
  result.body = "100M recycled.";
  CloudControlActionAdapter::Instance().ReportResult(result, 3);
}

TEST(CloudControlAction, RemoveAction) {
  CloudControlActionAdapter::Instance().RemoveAction(2002, 3);
  CloudControlActionAdapter::Instance().RemoveAction(2003, 3);
}

TEST(CloudControlAction, PcStatus) {
  std::stringstream ss;
  ss << "status info " << time(NULL);
  CloudControlActionAdapter::Instance().SetPcStatus("6b9185a70d3017aa109624844b6af671", ss.str(), 3);

  std::string status;
  CloudControlActionAdapter::Instance().GetPcStatus("6b9185a70d3017aa109624844b6af671", &status, 3);
}

struct Args {
  Args(int c, char ** v) : argc(c), argv(v) {}
  int argc;
  char** argv;
};
void * ThreadFun(void * p) {
  Args * args = (Args*)p;
  testing::InitGoogleTest(&args->argc, args->argv);
  return (void*)RUN_ALL_TESTS();
}

const int kThreadCount = 1;

int main(int argc, char** argv) {
  LOG_INIT("./cloud_control_action_adapter_test.log", "DEBUG");
  pthread_t ts[kThreadCount];

  Args args(argc, argv);
  for (int i = 0; i < kThreadCount; ++i) {
    pthread_create(&ts[i], NULL, ThreadFun, &args);
  }

  for (int i = 0; i < kThreadCount; ++i) {
    pthread_join(ts[i], NULL);
  }
  return 0;
}

